package com.ht.module.sys.controller;

import com.ht.config.log.annotation.SysLog;
import com.ht.constant.DicConstant;
import com.ht.module.sys.entity.SysDict;
import com.ht.module.sys.service.SysDictService;
import com.ht.util.CommMethod;
import com.ht.util.JsonResult;
import com.ht.util.Pager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author Auto-generator
 * @since 2021-05-19
 */
@Tag(name = "字典表 ")
@RestController
@RequestMapping("/sys-dict")
public class SysDictController {


    @Autowired
    private SysDictService sysDictService;


    @Operation(summary = "获取所有字典")
    @GetMapping("/findList")
    @PreAuthorize("hasAnyAuthority('SYS_DICT')")
    @SysLog(msg = "列表查询字典")
    public JsonResult findList(SysDict sysDict) {
        return JsonResult.success(sysDictService.findList(sysDict));
    }


    @Operation(summary = "分页查询")
    @GetMapping("/findPage")
    @PreAuthorize("hasAnyAuthority('SYS_DICT')")
    @SysLog(msg = "分页查询字典")
    public JsonResult findPage(Pager<SysDict> pager, SysDict sysDict) {
        return JsonResult.success(sysDictService.findPage(pager, sysDict));
    }


    @Operation(summary = "通过id查询")
    @DeleteMapping("/get-by-id/{id}")
    @SysLog(msg = "通过id查询字典")
    public JsonResult getById(@PathVariable(value = "id") String id) {
        return JsonResult.success(sysDictService.getById(id));
    }

    @Operation(summary = "新增")
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('SYS_DICT_ADD')")
    @SysLog(msg = "新增字典", type = DicConstant.OperLogType.ADD)
    public JsonResult add(@RequestBody @Validated SysDict sysDict) {
        CommMethod.beanCreate(sysDict);
        sysDictService.save(sysDict);
        return JsonResult.success();
    }


    @Operation(summary = "删除")
    @DeleteMapping("/delete-by-id/{id}")
    @PreAuthorize("hasAnyAuthority('SYS_DICT_DEL')")
    @SysLog(msg = "删除字典", type = DicConstant.OperLogType.DEL)
    public JsonResult delete(@PathVariable(value = "id") String id) {
        sysDictService.delById(id);
        return JsonResult.success();
    }

    @Operation(summary = "修改字典")
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('SYS_DICT_UPDATE')")
    @SysLog(msg = "修改字典", type = DicConstant.OperLogType.UPDATE)
    public JsonResult updateById(@RequestBody @Validated SysDict sysDict) {
        sysDictService.edit(sysDict);
        return JsonResult.success();
    }

}
